<?php
class Line_order_product_model extends CI_Model
{
    private $table = 'line_order_product';
    private $table_2 = 'line_base';
    private $table_3 = 'user';
    private $table_4 = 'user_detail';
    private $table_5 = 'line_product_reviews';

    public function insertLineOrderProduct($order_id, $line_price, $postData)
    {
        $data = array(
            'order_id'                => $order_id,
            'line_id'                 => $line_price->line_id,
            'line_name'               => isset($postData['line_name']) ? $postData['line_name'] : '',
            'start_date'              => $line_price->start_date,
            'travel_days'             => $postData['travel_days'],
            'adult_num'               => $postData['adult_num'],
            'children_num'            => $postData['children_num'],
            'adult_original_price'    => $line_price->adult_original_price,
            'adult_price'             => $line_price->adult_price,
            'children_original_price' => $line_price->children_original_price,
            'children_price'          => $line_price->children_price,
            'home_price'              => $postData['home_price'] ? $postData['home_price'] : '0',
            'amount_original_total'   => checkoutPayOriginalSubtotal($line_price, $postData['adult_num'], $postData['children_num']) + $postData['home_price'],
            'amount_total'            => checkoutPaySubtotal($line_price, $postData['adult_num'], $postData['children_num']) + $postData['home_price'],
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function getProductByOrderId($orderId)
    {
        $this->db->select('line_order_product.*, line_base.id as line_id, line_base.line_name, line_base.uid, line_base.travel_days,user.alias_name,user.user_name,user_detail.telephone,user_detail.cellphone,line_base.line_tel');
        $this->db->from($this->table.' as line_order_product');
        $this->db->join($this->table_2.' as line_base', 'line_base.id = line_order_product.line_id');
        $this->db->join($this->table_3.' as user','line_base.uid = user.uid');
        $this->db->join($this->table_4.' as user_detail','line_base.uid = user_detail.uid');
        $this->db->where('line_order_product.order_id', $orderId);
        return $this->db->get();
    }
    
    public function findByOrderId($orderId)
    {
        $this->db->where('order_id', $orderId);
        return $this->db->get($this->table);
    }
    
    public function getReviewByOrderId($orderId)
    {
        $this->db->select('line_order_product.line_id, line_order_product.line_name, line_order_product.travel_days, line_order_product.start_date, line_order_product.adult_num, line_order_product.children_num, line_order_product.amount_total, line_product_reviews.*');
        $this->db->from($this->table.' as line_order_product');
        $this->db->join($this->table_5.' as line_product_reviews', 'line_product_reviews.order_id = line_order_product.order_id');
        $this->db->where('line_product_reviews.order_id', $orderId);
        return $this->db->get();
    }
}